// 
// Entify project
//  
// Author:
//       Lauri Taimila <lauri@taimila.com>
// 
// Copyright (c) 2010 Lauri Taimila
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Text.RegularExpressions;

using MonoDevelop.Core.Serialization;

namespace MonoDevelop.EntifyEditor
{
	/// <summary>
	/// RelationModel defines one relation property of the entity. Relation can
	/// be single or multi relation to other entity type.
	/// </summary>
	[DataItem("Relation")]
	public class Relation
	{
		/// <summary>
		/// Constuctor for XML Serializer.
		/// </summary>
		private Relation()
		{
			this.Name = "";
			this.Description = "";
			this.PersistentName = "";
			this.ToMany = false;
			this.Type = "";
		}
		
		/// <summary>
		/// Creates Relation instance with given name.
		/// </summary>
		public Relation(string name)
		{
			this.Name = name;
			this.Description = "";
			this.PersistentName = name;
			this.ToMany = false;
			this.Type = "";
		}

		/// <summary>
		/// Name of the realtion. This is used as the property name in the generated entity class.
		/// </summary>
		[ItemProperty]
		public string Name
		{
			get;
			set;
		}
		
		/// <summary>
		/// Persistent name of the relation. This must be unique within one entity type.
		/// Peristent name is used by persistent store when serializing the data.
		/// </summary>
		[ItemProperty]
		public string PersistentName
		{
			get;
			set;
		}
		
		/// <summary>
		/// Generic description text for relation. This is added as a property comment
		/// when generating code, thus it will be displayed by code completion tools.
		/// </summary>
		[ItemProperty]
		public string Description
		{
			get;
			set;
		}
		
		/// <summary>
		/// Defines is this relation To-Many relation.
		/// </summary>
		[ItemProperty]
		public bool ToMany
		{
			get;
			set;
		}
		
		/// <summary>
		/// Type of the related entity / entities. Contains
		/// GUID identifier of the related entity type.
		/// </summary>
		[ItemProperty]
		public string Type
		{
			get;
			set;
		}

		/// <summary>
		/// Is relation valid for code generation.
		/// </summary>
		public bool IsValid
		{
			get
			{
				if(this.IsRelationNameValid() && !string.IsNullOrEmpty(this.Type))
				{
					return true;
				}
				else
				{
					return false;
				}
			}
		}

		/// <summary>
		/// Is relation name valid to be class name?
		/// </summary>
		public bool IsRelationNameValid()
		{
			Regex regularExpression = new Regex("^[A-Z]+");
			var provider = System.CodeDom.Compiler.CodeDomProvider.CreateProvider("C#");
			
			return provider.IsValidIdentifier(this.Name) && regularExpression.IsMatch(this.Name);
		}
	}
}
